package com.service.menu;

import com.common.CollectionUtil;
import com.common.ResultUtil;
import com.entity.Menu;
import com.entity.Role2Menu;
import com.facade.menu.IMenuFacadeCUD;
import com.facade.menu.IMenuFacadeR;
import com.facade.role.IRole2MenuFacadeR;
import com.mapper.CustomMapper;
import com.mapper.MenuMapper;
import com.service.CustomServiceCUD;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
public class MenuServiceCUD extends CustomServiceCUD<Menu> implements IMenuFacadeCUD {

    @Resource
    private MenuMapper mapper;
    @Resource
    private IMenuFacadeR menuFacadeR;
    @Resource
    private IRole2MenuFacadeR role2MenuFacadeR;

    @Override
    protected CustomMapper<Menu> initMapper() {
        return mapper;
    }

    @Override
    public ResultUtil addMenu(Menu menu, Serializable... managerId) {
        Menu _$0 = menuFacadeR.findUniqueByPropertyEqualTo("name", menu.getName());
        ResultUtil resultUtil = ResultUtil.newInstance();
        if (null != _$0) {
            resultUtil.setStatus(ResultUtil.PARAM_VALID_FAILED);
            return resultUtil.setMessage("菜单名称已存在");
        }
        Long pid = menu.getPid();
        if (null != pid) {
            menu.setFullIds(menuFacadeR.findFullPropertyText(pid, "pid", "id", ",", true));
        }
        if (null == menu.getSeq()) {
            menu.setSeq(0);
        }
        addSelective(menu);
//        if (null != managerId && managerId.length > 0) {
//            String message = "添加菜单：[" + PojoUtil.notNull(menu, "", "id") + "]";
//            operationLogFacadeCUD.addOperationLog(managerId[0], OperationLog.ADD, message);
//        }
        resultUtil.setMessage("添加成功");
        return resultUtil;
    }

    @Override
    public ResultUtil modifyMenu(Menu menu, Serializable... managerId) {
        Long id = menu.getId();
        ResultUtil resultUtil = ResultUtil.newInstance();
        if (null != id) {
            Menu _$0 = menuFacadeR.findByPrimaryKey(id);
            if (null == _$0) {
                resultUtil.setStatus(ResultUtil.FAILED);
                return resultUtil.setMessage("记录不存在或已被删除");
            }
            Menu _$1 = menuFacadeR.findUniqueByPropertyEqualTo("name", menu.getName());
            if (null != _$1 && !_$1.getId().equals(id)) {
                resultUtil.setStatus(ResultUtil.PARAM_VALID_FAILED);
                return resultUtil.setMessage("菜单名称已存在");
            }
            Long pid = menu.getPid();
            if (null != pid && !pid.equals(_$0.getPid())) {
                menu.setFullIds(menuFacadeR.findFullPropertyText(pid, "pid", "id", ",", true));
            }
            menu.setUpdated(new Date());
            update(menu);
//            if (null != managerId && managerId.length > 0) {
//                String message = "修改菜单：[" + PojoUtil.compareDifferent(_$0, menu, "", "id", "name") + "]";
//                operationLogFacadeCUD.addOperationLog(managerId[0], OperationLog.MODIFY, message);
//            }
            resultUtil.setMessage("修改成功");
        }
        return resultUtil;
    }

    @Override
    public ResultUtil deleteMenu(Serializable id, Serializable... managerId) {
        Menu menu = menuFacadeR.findByPrimaryKey(id);
        ResultUtil resultUtil = ResultUtil.newInstance();
        if (null == menu) {
            resultUtil.setStatus(ResultUtil.FAILED);
            return resultUtil.setMessage("记录不存在或已被删除");
        }
        List<Menu> menuList = menuFacadeR.findByPropertyEqualTo("pid", id);
        if (!CollectionUtil.isEmpty(menuList)) {
            resultUtil.setStatus(ResultUtil.FAILED);
            return resultUtil.setMessage("父类菜单不能删除");
        }
        List<Role2Menu> role2MenuList = role2MenuFacadeR.findByPropertyEqualTo("menuId", id);
        if (!CollectionUtil.isEmpty(role2MenuList)) {
            resultUtil.setStatus(ResultUtil.FAILED);
            return resultUtil.setMessage("菜单被使用不能删除");
        }
        resultUtil.setNumber(deleteByPrimaryKey(id));
//        if (null != managerId && managerId.length > 0) {
//            String message = "删除菜单：[" + PojoUtil.notNull(menu, "", "id", "name") + "]";
//            operationLogFacadeCUD.addOperationLog(managerId[0], OperationLog.DELETE, message);
//        }
        resultUtil.setMessage("删除成功");
        return resultUtil;
    }
}
